# Define registry path and value name
$regPath = "HKLM:\SOFTWARE\WOW6432Node\FileWave\WinClient"
$valueName = "fwUser"

try {
    # Attempt to read the fwUser registry value
    $fwClientName = (Get-ItemProperty -Path $regPath -Name $valueName).$valueName
} catch {
    Write-Error "Failed to read registry value '$valueName' from '$regPath'"
    exit 1
}

# Get the current computer name
$currentName = $env:COMPUTERNAME

# Compare current name with the fwClientName from registry
if ($fwClientName -ieq $currentName) {
    # Names match (case-insensitive); do nothing and exit with code 1
    exit 1
} else {
    try {
        # Attempt to rename the computer
        Rename-Computer -NewName $fwClientName -Force -ErrorAction Stop

        # Notify user and schedule reboot in 60 seconds
        shutdown.exe /r /t 60 /c "Computer renamed to $fwClientName. Rebooting in 60 seconds." /f /d p:4:1

        # Exit with code 2 to indicate rename and reboot initiated
        exit 2
    } catch {
        Write-Error "Failed to rename the computer to '$fwClientName'. Error: $_"
        exit 3
    }
}
